using System;
using System.Collections.Generic;
using System.Text;

namespace Antix.Data.Catalogue.Objects
{
    /// <summary>
    /// Database Index
    /// </summary>
    public class DatabaseIndex : DatabaseObject
    {
        #region Constructor/Defaults

        /// <summary>
        /// Create a new index with defaults
        /// </summary>
        internal DatabaseIndex(DatabaseTable table)
            : this("(new index)", null, false, table)
        {
            base.IsNew = true;
        }

        /// <summary>
        /// Create an existing column object
        /// </summary>
        internal DatabaseIndex(
            string nameOriginal,
            string columns, bool isUnique,
            DatabaseTable table)
            : base(nameOriginal)
        {
            Table = table;
            _Columns = columns;
            _IsUnique = isUnique;
        }

        #endregion

        #region Properties

        #region Key

        /// <summary>
        /// Key for this object
        /// </summary>
        public override string Key
        {
            get { return this.Name; }
        }

        #endregion

        public DatabaseTable Table{get;private set;}

        #region Columns

        private string _Columns;

        /// <summary>
        /// Columns in the index eg "Name, DateOfBirth DESC"
        /// </summary>
        public string Columns
        {
            get { return _Columns; }
            set
            {
                if (_Columns != value)
                {
                    _Columns = value;
                    base.IsEdited = true;
                }
            }
        }

        #endregion

        #region IsUnique

        private bool _IsUnique;

        /// <summary>
        /// Index is unique
        /// </summary>
        public bool IsUnique
        {
            get { return _IsUnique; }
            set
            {
                if (_IsUnique != value)
                {
                    _IsUnique = value;
                    base.IsEdited = true;
                }
            }
        }

        #endregion

        #endregion

        #region Methods

        #region Script the index

        /// <summary>
        /// Generate script for this index
        /// </summary>
        /// <param name="sql">Sql</param>
        public override bool Script(StringBuilder sql, bool subObjects)
        {
            return this.Table.Database.Script(this, sql, subObjects);
        }


        #endregion

        #endregion
    }
}